import {deleteState} from '../localStorages/localStorageUesrData.js';
import {
    TheUserLogsOut, UpdatePassword, UpdateUserBriefIntroduction, UpdateUserEmail, UpdateUserName
} from "../Axios/updtaUser.js"
import {showToast} from "../Toast.js"

import {onInit} from "./file.js";
import {InitInformation} from "./DataRendering.js";
// 退出账户
document.getElementById("exitAccount").addEventListener("click", () => {
    TheUserLogsOut({ConfirmDelete: true}).then(r => window.location.replace('/login.html'))
    deleteState();
});

// 修改密码
document.getElementById("ConfirmChangePassword").addEventListener("click", e => {

    const oldPassword = document.getElementById('oldPassword').value;
    const newPassword = document.getElementById('newPassword').value;
    UpdatePassword({
        oldPassword, newPassword
    }).then(e => {
        showToast("修改成功", "success")

        window.location.replace('/login.html')
    }).catch(ex => {
        console.log(ex.response.data.message);
        showToast(ex.response.data.message, "error")
    })
    e.preventDefault();

});

//修改用户名称
document.getElementById("ModifyUserName").addEventListener("click", e => {
    const oldUsername = document.getElementById('oldUsername').value;
    const newUsername = document.getElementById('newUsername').value;

    UpdateUserName({
        oldUsername, newUsername
    }).then(e => {
        showToast("修改成功", "success")

        window.location.replace('/login.html')
    }).catch(ex => {
        showToast(ex.response.data.message, "error")
    })

    showToast("修改用户名称测试", "error")
    e.preventDefault();
})

///修改用户邮箱
document.getElementById("ModifyUserEmail").addEventListener("click", e => {
    const oldUsername = document.getElementById('oldEmail').value;
    const newUsername = document.getElementById('newEmail').value;

    UpdateUserEmail({
        oldUsername, newUsername
    }).then(e => {
        showToast("修改成功", "success")

        window.location.replace('/login.html')
    }).catch(ex => {
        showToast(ex.response.data.message, "error")
    })

    e.preventDefault();
    showToast("修改用户邮箱测试", "error")
})

//修改个人简介
document.getElementById("ModifyBiography").addEventListener("click", e => {
    const newIntroduction = document.getElementById('newIntroduction').value;


    document.getElementById('introduction').innerText = `简介: ${newIntroduction || "这个人没有写简介"}`;

    UpdateUserBriefIntroduction({
        introduction: newIntroduction
    }).then(() => {
        showToast("修改成功", "success")
    }).catch(ex => {
        showToast(ex.response.data.message, "error")
    })

    e.preventDefault();
    showToast("修改个人简介测试", "error")
})


window.onload = function () {
    onInit('./images/image-logo.webp')
    InitInformation();
};